package utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * This class contains the list of elements that should be removed when
 * parsing a file to index.
 */
public class StopList
{
  /** The stop list. */
  private List<String> stopList;
  
  /**
   * Instantiates and load the stop list.
   */
  public StopList()
  {
    loadStopList();
  }
  
  /**
   * Tests if the stop list contains the given word
   *
   * @param word the word to test
   * @return true, if the word is in the stoplist
   */
  public boolean contains(String word)
  {
    return word != null && stopList.contains(word);
  }

  /**
   * Load the stop list from the file
   */
  private void loadStopList()
  {
    stopList = new ArrayList<>();
    try
    {
      // Récupère tous les mots de la stop liste depuis le fichier
      InputStream stopListFileInputStream = getClass().getResourceAsStream("/utils/stopliste.txt");
      InputStreamReader ipStreamReader = new InputStreamReader(stopListFileInputStream);
      BufferedReader buffReader = new BufferedReader(ipStreamReader);

      String line;
      while ((line = buffReader.readLine()) != null)
      {
        stopList.add(line);
      }
      
      stopListFileInputStream.close();
      ipStreamReader.close();
      buffReader.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}
